package com.zhiyou100.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class UrlFilter implements Filter{

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest) req;
		Object obj = request.getServletContext().getAttribute("basePath");
		if(obj==null){
			String basePath=req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+request.getContextPath()+"/";
			System.out.println(basePath);
			//共享到上下文中
			request.getServletContext().setAttribute("basePath", basePath);
		}
		chain.doFilter(request, resp);
	}

}
